package com.rizhov.main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

public class ClientList {

    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private PushbackInputStream pis;

    private Integer tempInt = new Integer(-1);

    public ClientList(ObjectOutputStream oos, ObjectInputStream ois, PushbackInputStream pis) {
	this.oos = oos;
	this.ois = ois;
	this.pis = pis;
    }

    public boolean add(String tempString) throws IOException {
	ArrayList<Object> arrList = new ArrayList();
	arrList.add(new Integer(CommandDescriptor.ADD_STRING.getCode()));
	arrList.add(tempString);
	oos.writeObject(arrList);
	oos.flush();
	tempInt = ois.readInt();
	return tempInt == 1 ? true : false;
    }

    public boolean remove(String tempString) throws IOException {
	ArrayList<Object> arrList = new ArrayList();
	arrList.add(new Integer(CommandDescriptor.REMOVE_STRING.getCode()));
	arrList.add(tempString);
	oos.writeObject(arrList);
	oos.flush();
	int result = ois.readInt();
	return result == 1 ? true : false;
    }

    public int size() throws IOException {
	ArrayList<Object> arrList = new ArrayList();
	arrList.add(new Integer(CommandDescriptor.GET_SIZE_OF_COLLECTION.getCode()));
	oos.writeObject(arrList);
	oos.flush();
	return ois.readInt();
    }

    /**
     * 
     * @param index
     *            index in collection
     * @return the string selected by index or null if there is no such entry in
     *         collection
     * @throws IOException
     */
    public String get(int index) throws IOException {
	ArrayList<Object> tempList = new ArrayList<Object>();
	tempList.add(new Integer(CommandDescriptor.GET_BY_INDEX.getCode()));
	tempList.add(new Integer(index));
	oos.writeObject(tempList);
	oos.flush();
	String result = null;
	try {
	    result = (String) ois.readObject();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}
	if (result.equals("null") || result == null) {
	    return null;
	} else {
	    return result;
	}
    }

    public List<String> getAllTheList() {
	ArrayList<Object> tempList = new ArrayList<Object>();
	tempList.add(new Integer(CommandDescriptor.GET_ALL_THE_LIST.getCode()));
	try {
	    oos.writeObject(tempList);
	    oos.flush();
	} catch (IOException e1) {
	    e1.printStackTrace();
	}
	Object resultList = null;
	try {
	    Object temp = ois.readObject();
	    if (temp instanceof String) {
		resultList = temp;
	    } else {
		resultList = temp;
	    }
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	if (resultList.equals("null") || resultList == null) {
	    return null;
	} else {
	    return (ArrayList<String>) resultList;
	}
    }
}
